package com.jstyle.beatecg.adapter;

import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jstyle.beatecg.R;
import com.jstyle.beatecg.model.PathRecord;
import com.jstyle.beatecg.util.DateUtil;

import java.text.NumberFormat;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.http.HEAD;

/**
 * Created by Administrator on 2017/12/11.
 */

public class HistoryDetailAdapter extends RecyclerView.Adapter {
    private PathRecord pathRecord;
    private static final String TAG = "HistoryDetailAdapter";
    public HistoryDetailAdapter(PathRecord pathRecord) {
        this.pathRecord = pathRecord;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_history_detail, null);
        return new HistoryDetailViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        HistoryDetailViewHolder historyDetailViewHolder = (HistoryDetailViewHolder) holder;
        Resources resources = holder.itemView.getContext().getResources();
        String[] array = resources.getStringArray(R.array.history_detail_array);
        historyDetailViewHolder.tvTitle.setText(array[position]);
        int id = 0;
        String data = "";
        if (position == 0) {
            float durationTime=Float.valueOf(pathRecord.getMDuration());
            data= DateUtil.getSportTime((int) durationTime);
            id = R.drawable.history_time;
        } else if (position == 1) {
            data = pathRecord.getCalString();
            id = R.drawable.history_cal;
        } else if (position == 2) {
            float distance = Float.valueOf(pathRecord.getDistanceBeat()) / 100f;
            data = String.valueOf(distance);
            id = R.drawable.history_distance;
        } else if (position == 3) {
            id = R.drawable.history_heartrate;
        } else if (position == 4 || position == 6) {
            id = R.drawable.history_heart_average;
            String heartList = pathRecord.getHeartListString();
            if (TextUtils.isEmpty(heartList)) {
                data = 0 + "";
            } else {
                String[]hearts=heartList.split(",");
                int max=0;
                int total=0;
                int length=hearts.length;
                for(String heart:hearts){
                    float heartValue=Float.valueOf(heart);
                    max= (int) Math.max(max,heartValue);
                    total+=heartValue;
                }
                if (position == 4) {
                    data=String.valueOf(total/length);
                } else if (position == 6) {
                    data=String.valueOf(max);
                }
            }
        } else if (position == 5 || position == 7) {
            id = R.drawable.history_step;
            String heartList = pathRecord.getSpeedListString();
            Log.i(TAG, "onBindViewHolder: "+heartList);
            NumberFormat numberFormat=NumberFormat.getNumberInstance();
            numberFormat.setMaximumFractionDigits(1);
            if (TextUtils.isEmpty(heartList)) {
                data = 0 + "";
            } else {
                String[]hearts=heartList.split(",");
                float max=0;
                float total=0;
                int length=hearts.length;
                for(String heart:hearts){
                    float heartValue=(Float.valueOf(heart)-76)/5;
                    max=  Math.max(max,heartValue);
                    total+=heartValue;
                }
                if (position == 5) {
                    data=numberFormat.format(total/length);
                } else if (position == 7) {
                    data=numberFormat.format(max);
                }
            }
        }
        historyDetailViewHolder.tvData.setText(data);
        Drawable drawable = resources.getDrawable(id);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        historyDetailViewHolder.tvTitle.setCompoundDrawables(drawable, null, null, null);
    }


    @Override
    public int getItemCount() {
        return 8;
    }

    class HistoryDetailViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.bt_history_time)
        public TextView tvTitle;
        @BindView(R.id.tv_history_time)
        public TextView tvData;

        public HistoryDetailViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
